package com.lyandwc.lw_bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyandwc.lw_bbs.dao.NotificationDao;
import com.lyandwc.lw_bbs.entity.Notification;
import com.lyandwc.lw_bbs.service.NotificationService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationDao, Notification> implements NotificationService {
    @Override
    public List<Notification> listNotificationsByUid(int userId) {
        return this.baseMapper.listNotificationsByUid(userId);
    }

    @Override
    public List<Notification> listUnreadNotificationsByUid(int userId) {
        return this.baseMapper.listUnreadNotificationsByUid(userId);
    }

    @Override
    public Boolean checkIsOwner(int id, int userId) {
        return this.baseMapper.checkIsOwner(id, userId);
    }

    @Override
    public Notification addNotification(int action, int commentId, int userId, int fromCommentId, int fromUserId) {
        Notification notification = new Notification();

        if (action == 0) {
            notification.setFromCommentId(fromCommentId);
        }
        notification.setAction(action);
        notification.setCreateAt(new Timestamp(System.currentTimeMillis()));
        notification.setCommentId(commentId);
        notification.setUserId(userId);
        notification.setFromUserId(fromUserId);

        this.save(notification);

        return notification;
    }
}
